package com.ruoyi.mtAudit.service.impl;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.mateCheck.domain.MateCheck;
import com.ruoyi.mateCheck.mapper.MateCheckMapper;
import com.ruoyi.mateCheck.mapper.MateTestMapper;
import com.ruoyi.mateCheck.service.MateCheckService;
import com.ruoyi.mateDevice.mapper.MateDeviceMapper;
import com.ruoyi.mateDevice.service.MateDeviceService;
import com.ruoyi.mateInventory.domain.MateInventory;
import com.ruoyi.mateInventory.domain.MateInventoryModel;
import com.ruoyi.mateInventory.mapper.MateInventoryMapper;
import com.ruoyi.mateProcurement.mapper.MateProcurementMapper;
import com.ruoyi.mateWarehousing.domain.MateWarehousing;
import com.ruoyi.mateproduction.domain.MateCleanRecord;
import com.ruoyi.mateproduction.domain.MateDaily;
import com.ruoyi.mateproduction.domain.MateProductionPlan;
import com.ruoyi.mateproduction.domain.MateProductionRecord;
import com.ruoyi.mateproduction.domain.MateRecordInformation;
import com.ruoyi.mateproduction.domain.MateTransfer;
import com.ruoyi.mateproduction.domain.MtCourseCheck;
import com.ruoyi.mateproduction.mapper.MateCleanRecordMapper;
import com.ruoyi.mateproduction.mapper.MateCourseMapper;
import com.ruoyi.mateproduction.mapper.MateDailyMapper;
import com.ruoyi.mateproduction.mapper.MateTransferMapper;
import com.ruoyi.mateproduction.mapper.MtProductionMapper;
import com.ruoyi.mateproduction.mapper.MtRecordMapper;
import com.ruoyi.mateRequistion.domain.MateRequistion;
import com.ruoyi.mateRequistion.domain.MateRequistionModel;
import com.ruoyi.mateRequistion.mapper.MateRequistionMapper;
import com.ruoyi.mateThrough.domain.MateThrough;
import com.ruoyi.mateThrough.mapper.MateThroughMapper;
import com.ruoyi.mateWarehousing.mapper.MateWarehousingMapper;
import com.ruoyi.material.domain.MateProductCheck;
import com.ruoyi.material.mapper.CheckReportMapper;
import com.ruoyi.material.mapper.MateProductMapper;
import com.ruoyi.mtAudit.domain.MtAudit;
import com.ruoyi.mtAudit.mapper.MtAuditMapper;
import com.ruoyi.mtAudit.service.MtAuditService;
import com.ruoyi.productCheck.domain.CheckInformation;
import com.ruoyi.productCheck.domain.ProductReport;
import com.ruoyi.productCheck.mapper.CheckInformationMapper;
import com.ruoyi.productCheck.mapper.ProductReportMapper;
import com.ruoyi.readyProcurement.domain.ReadyProcurement;
import com.ruoyi.readyProcurement.mapper.ReadyMapper;
import com.ruoyi.warehousing.mapper.WarehousingRecordsMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author
 * @Version 1.0
 */
@Service
public class MtAuditServiceImpl implements MtAuditService {

    @Autowired
    MtAuditMapper mtAuditMapper;

    @Autowired
    MateCheckMapper mateCheckMapper;

    @Autowired
    MateCheckService mateCheckService;

    @Autowired
    MateDeviceService mateDeviceService;

    @Autowired
    MateDeviceMapper mateDeviceMapper;

    @Autowired
    MateProductMapper mateProductMapper;

    @Autowired
    MateInventoryMapper mateInventoryMapper;

    @Autowired
    CheckReportMapper checkReportMapper;

    @Autowired
    MateRequistionMapper mateRequistionMapper;

    @Autowired
    MateProcurementMapper mateProcurementMapper;

    @Autowired
    MateThroughMapper mateThroughMapper;

    @Autowired
    MtProductionMapper mtProductionMapper;

    @Autowired
    MateWarehousingMapper warehousingMapper;

    @Autowired
    WarehousingRecordsMapper warehousingRecordsMapper;

    @Autowired
    MtRecordMapper mtRecordMapper;

    @Autowired
    MateDailyMapper mateDailyMapper;

    @Autowired
    MateCourseMapper mateCourseMapper;

    @Autowired
    MateTransferMapper mateTransferMapper;

    @Autowired
    MateCleanRecordMapper mateCleanRecordMapper;

    @Autowired
    CheckInformationMapper checkInformationMapper;

    @Autowired
    ReadyMapper readyMapper;

    @Autowired
    MateTestMapper mateTestMapper;

    @Autowired
    ProductReportMapper productReportMapper;

    @Override
    public MtAudit selectAuditById(Long auditId) {
        return mtAuditMapper.selectAuditById(auditId);
    }

    @Override
    public int insertMtAudit(MtAudit mtAudit) {
        return mtAuditMapper.insertMtAudit(mtAudit);
    }

    @Override
    public MtAudit selectAuditByIdIn(MtAudit mtAudit) {
        return mtAuditMapper.selectAuditByIdIn(mtAudit);
    }

    @Override
    public int updateAudit(MtAudit mtAudit, Long deptId) {
        SysUser sysUser = SecurityUtils.getLoginUser().getUser();
        MtAudit mtAudit1 = mtAuditMapper.selectAuditByIdIn(mtAudit);
        if (mtAudit1.getAuditStatus() != null && !mtAudit1.getAuditStatus().equals("")) {
            mtAudit1.setAuditStatus(mtAudit.getAuditStatus());
        }
        if (mtAudit.getAuditRemark() != null && !mtAudit.getAuditRemark().equals("")) {
            mtAudit1.setAuditRemark(mtAudit.getAuditRemark());
        }
        mtAudit1.setAuditBy(mtAudit.getAuditBy());
        mtAudit1.setAuditTime(mtAudit.getAuditTime());
        int num = 0;
        if (mtAudit1.getOrderType().equals("0")) {
            if ("1".equals(mtAudit.getAuditStatus())) {
                MateCheck mateCheck = mateCheckMapper.selectMateCheckByOrderId(mtAudit1.getOrderId());
                mateCheck.setCheckStatus(mtAudit.getAuditStatus());
                if (mtAudit.getAuditStatus() != null && mtAudit.getAuditStatus().equals("")) {
                    mtAudit.setAuditTime(DateUtils.getNowDate());
                }
                num = mateCheckMapper.updateCheckInformation(mateCheck);
                ReadyProcurement readyProcurement = new ReadyProcurement();
                readyProcurement.setIsCreatCheck("1");
                readyProcurement.setReadyId(mateCheck.getReadyId());
                readyMapper.updateReadyProcurement(readyProcurement);
            } else {
                MateCheck mateCheck = mateCheckMapper.selectMateCheckByOrderId(mtAudit1.getOrderId());
                ReadyProcurement readyProcurement = new ReadyProcurement();
                readyProcurement.setIsCreatCheck("0");
                readyProcurement.setReadyId(mateCheck.getReadyId());
                readyMapper.updateReadyProcurement(readyProcurement);
                mateCheck.setCheckStatus(mtAudit.getAuditStatus());
                mateTestMapper.removeTestRecord(mateCheck.getCkInformationId());
                mateDeviceMapper.deleteUserDevice(mateCheck.getCkInformationId());
                num = mateCheckMapper.updateCheckInformation(mateCheck);
            }
        } else if (mtAudit1.getOrderType().equals("1")) {
            if ("1".equals(mtAudit.getAuditStatus())) {
                MateProductCheck mateProductCheck = checkReportMapper.selectMateProductCheckById(mtAudit1.getOrderId());
                mateProductCheck.setMpcheckStatus(mtAudit.getAuditStatus());
                if (mtAudit.getAuditStatus() != null && mtAudit.getAuditStatus().equals("")) {
                    mtAudit.setAuditTime(DateUtils.getNowDate());
                }
                MateCheck mateCheck = mateCheckMapper.selectMateCheckByOrderId(mateProductCheck.getCkInformationId());
                mateCheck.setIsCreateProject("1");
                mateCheckMapper.updateCheckInformation(mateCheck);
                ReadyProcurement readyProcurement = new ReadyProcurement();
                readyProcurement.setAuditCheck("1");
                readyProcurement.setReadyId(mateCheck.getReadyId());
                readyMapper.updateReadyProcurement(readyProcurement);
                num = checkReportMapper.updateMateProductCheck(mateProductCheck);
            } else {
                MateProductCheck mateProductCheck = checkReportMapper.selectMateProductCheckById(mtAudit1.getOrderId());
                mateProductCheck.setMpcheckStatus(mtAudit.getAuditStatus());
                MateCheck mateCheck = mateCheckMapper.selectMateCheckByOrderId(mateProductCheck.getCkInformationId());
                mateCheck.setIsCreateProject("0");
                mateCheck.setCkInformationId(mateProductCheck.getCkInformationId());
                mateCheckMapper.updateCheckInformation(mateCheck);
                ReadyProcurement readyProcurement = new ReadyProcurement();
                readyProcurement.setAuditCheck("0");
                readyProcurement.setReadyId(mateCheck.getReadyId());
                readyMapper.updateReadyProcurement(readyProcurement);
                num = checkReportMapper.updateMateProductCheck(mateProductCheck);
            }
        } else if (mtAudit1.getOrderType().equals("3")) {
            //采购计划和原材料清单用的是同一张表,驳回的时候修改状态原材料清单状态也被修改
            if ("1".equals(mtAudit.getAuditStatus())) {
                MateInventory mateInventory = mateInventoryMapper.selectEdit(mtAudit1.getOrderId());
                mateInventory.setInventoryStatus(mtAudit.getAuditStatus());
                if (mtAudit.getAuditStatus() != null && mtAudit.getAuditStatus().equals("")) {
                    mtAudit.setAuditTime(DateUtils.getNowDate());
                }
                num = mateInventoryMapper.updateMateInventory(mateInventory);

            } else {
                MateInventory mateInventory = mateInventoryMapper.selectEdit(mtAudit1.getOrderId());
                mateInventory.setInventoryStatus(mtAudit.getAuditStatus());
                num = mateInventoryMapper.updateMateInventory(mateInventory);
            }
        } else if (mtAudit1.getOrderType().equals("5")) {
            if ("1".equals(mtAudit.getAuditStatus())) {
                MateRequistion mateRequistion = mateRequistionMapper.selectMateRequistion(mtAudit1.getOrderId());
                mateRequistion.setRqStatus(mtAudit.getAuditStatus());
                if (mtAudit.getAuditStatus() != null && mtAudit.getAuditStatus().equals("")) {
                    mtAudit.setAuditTime(DateUtils.getNowDate());
                }
                num = mateRequistionMapper.updateMateRequistion(mateRequistion);
            } else {
                MateRequistion mateRequistion = mateRequistionMapper.selectMateRequistion(mtAudit1.getOrderId());
                mateRequistion.setRqStatus(mtAudit.getAuditStatus());
                num = mateRequistionMapper.updateMateRequistion(mateRequistion);
            }
        } else if (mtAudit1.getOrderType().equals("6")) {
            if ("1".equals(mtAudit.getAuditStatus())) {
                MateThrough mateThrough = mateThroughMapper.selectMateThrough(mtAudit1.getOrderId());
                mateThrough.setThroughStatus(mtAudit.getAuditStatus());
                if (mtAudit.getAuditStatus() != null && mtAudit.getAuditStatus().equals("")) {
                    mtAudit.setAuditTime(DateUtils.getNowDate());
                }
                List<MateRequistionModel> list = mateRequistionMapper.selectMateRequistionModel(mateThrough.getRequistionModelId());
                for (MateRequistionModel mateRequistionModel : list) {
                   // int realityNum = mateRequistionModel.getRealityNum();

                /*
                    WarehousingRecords warehousingRecords = new WarehousingRecords();
                    String productType = mateRequistionModel.getMateProductType();
                    Long productId = mateRequistionModel.getMateproductId();
                    String batch = mateRequistionModel.getMateBatch();
                    MateWarehousing mateWarehousing = new MateWarehousing();
                    mateWarehousing.setDeptId(deptId);
                    mateWarehousing.setMateProductType(productType);
                    mateWarehousing.setMateproductId(productId);
                    mateWarehousing.setMateBatch(batch);
                    MateWarehousing mateWarehousing1 = warehousingMapper.selectMateWarehousing(mateWarehousing);
                    warehousingRecords.setInventoryNum(-realityNum);
                    warehousingRecords.setAllnum(-realityNum);
                    if (mateWarehousing1 == null) {
                        warehousingMapper.insertMateWarehousing(mateWarehousing);
                    } else {
                        int warehousingNum = mateWarehousing1.getWarehousingNum();
                        warehousingNum = warehousingNum - realityNum;
                        mateWarehousing.setWarehousingNum(warehousingNum);
                        mateWarehousing.setWarehousingId(mateWarehousing1.getWarehousingId());
                        warehousingMapper.updateMateWarehousing(mateWarehousing);
                    }
                    Long warehousingId = mateWarehousing.getWarehousingId();
                    warehousingRecords.setOrderNum(mateRequistionModel.getRequistionModelId().toString());
                    warehousingRecords.setWarehousingId(warehousingId);
                    warehousingRecords.setSupplierId(mateRequistionModel.getMaterialId());
                    warehousingRecords.setInventoryStatus("9");
                    warehousingRecords.setCreateBy(SecurityUtils.getLoginUser().getUser().getNickName());
                    warehousingRecordsMapper.insertWarehousingRecords(warehousingRecords);*/
                   // mateRequistionModel.setRealityNum(realityNum - mateThrough.getRealityNum());
                    mateRequistionMapper.updateMateRequistionModel(mateRequistionModel);
                }
                num = mateThroughMapper.updatemateThrough(mateThrough);

            } else {
                MateThrough mateThrough = mateThroughMapper.selectMateThrough(mtAudit1.getOrderId());
                mateThrough.setThroughStatus(mtAudit.getAuditStatus());
                num = mateThroughMapper.updatemateThrough(mateThrough);
            }

        } else if (mtAudit1.getOrderType().equals("7")) {
            if (mtAudit.getAuditStatus().equals("1")) {
                MateProductionPlan mateProductionPlan = mtProductionMapper.selectPlan(mtAudit1.getOrderId());
                mateProductionPlan.setPlanStatus(mtAudit.getAuditStatus());
                if (mtAudit.getAuditStatus() != null && mtAudit.getAuditStatus().equals("")) {
                    mtAudit.setAuditTime(DateUtils.getNowDate());
                }
                num = mtProductionMapper.editPlan(mateProductionPlan);
            } else {
                MateProductionPlan mateProductionPlan = mtProductionMapper.selectPlan(mtAudit1.getOrderId());
                mateProductionPlan.setPlanStatus(mtAudit.getAuditStatus());
                num = mtProductionMapper.editPlan(mateProductionPlan);
            }
        } else if (mtAudit1.getOrderType().equals("8")) {
            if (mtAudit.getAuditStatus().equals("1")) {
                MateProductionRecord mateProductionRecord = mtRecordMapper.selectRecord(mtAudit1.getOrderId());
                if (mtAudit.getAuditStatus() != null && mtAudit.getAuditStatus().equals("")) {
                    mtAudit.setAuditTime(DateUtils.getNowDate());
                }
                MateWarehousing mateWarehousing = new MateWarehousing();
                for (MateRecordInformation mateRecordInformation : mateProductionRecord.getMateRecordInformations()) {
                    String mateproductType = mateRecordInformation.getMateProductType();
                    String enterNum = mateRecordInformation.getEnterNum();
                    int allNum = Math.toIntExact(mateRecordInformation.getNeedNum());
                    mateWarehousing.setDeptId(deptId);
                    mateWarehousing.setEnterNum(enterNum);
                    mateWarehousing.setMateProductType(mateproductType);
                    mateWarehousing.setWarehousingId(mateRecordInformation.getWarehousingId());
                    mateWarehousing.setMateproductId(mateRecordInformation.getMateproductId());
                    mateWarehousing.setWarehousingNum(allNum);
                    MateWarehousing mateWarehousing1 = warehousingMapper.selectMateWarehousing(mateWarehousing);
                    if (mateWarehousing1 == null) {
                        warehousingMapper.insertMateWarehousing(mateWarehousing);
                    } else {
                        int warehousingNum = mateWarehousing1.getWarehousingNum();
                        warehousingNum = warehousingNum - allNum;
                        mateWarehousing.setWarehousingNum(warehousingNum);
                        mateWarehousing.setWarehousingId(mateWarehousing1.getWarehousingId());
                        warehousingMapper.updateMateWarehousing(mateWarehousing);
                    }
                }

                mateProductionRecord.setRecordStatus(mtAudit.getAuditStatus());
                num = mtRecordMapper.editRecord(mateProductionRecord);
                MateTransfer mateTransfer = new MateTransfer();
                mateTransfer.setTransferId(mateProductionRecord.getTransferId());
                mateTransfer.setIsCreateRecord("1");
                mateTransferMapper.updateMateTransfer(mateTransfer);
            } else {
                MateProductionRecord mateProductionRecord = mtRecordMapper.selectRecord(mtAudit1.getOrderId());
                mateProductionRecord.setRecordStatus(mtAudit.getAuditStatus());
                num = mtRecordMapper.editRecord(mateProductionRecord);
                MateTransfer mateTransfer = new MateTransfer();
                mateTransfer.setTransferId(mateProductionRecord.getTransferId());
                mateTransfer.setIsCreateRecord("0");
                mateTransferMapper.updateMateTransfer(mateTransfer);
            }

        } else if (mtAudit1.getOrderType().equals("9")) {
            if (mtAudit.getAuditStatus().equals("1")) {
                MateDaily mateDaily = mateDailyMapper.selectMateDaily(mtAudit1.getOrderId());
                if (mtAudit.getAuditStatus() != null && mtAudit.getAuditStatus().equals("")) {
                    mtAudit.setAuditTime(DateUtils.getNowDate());
                }
                mateDaily.setDailyStatus(mtAudit.getAuditStatus());
                num = mateDailyMapper.editMatedaily(mateDaily);
            } else {
                MateDaily mateDaily = mateDailyMapper.selectMateDaily(mtAudit1.getOrderId());
                mateDaily.setDailyStatus(mtAudit.getAuditStatus());
                num = mateDailyMapper.editMatedaily(mateDaily);

            }

        } else if (mtAudit1.getOrderType().equals("10")) {
            if (mtAudit.getAuditStatus().equals("1")) {
                MtCourseCheck mtCourseCheck = mateCourseMapper.selectMateCourse(mtAudit1.getOrderId());
                if (mtAudit.getAuditStatus() != null && mtAudit.getAuditStatus().equals("")) {
                    mtAudit.setAuditTime(DateUtils.getNowDate());
                }
                mtCourseCheck.setCourseStatus(mtAudit.getAuditStatus());
                num = mateCourseMapper.updateCourse(mtCourseCheck);

            } else {
                MtCourseCheck mtCourseCheck = mateCourseMapper.selectMateCourse(mtAudit1.getOrderId());
                mtCourseCheck.setCourseStatus(mtAudit.getAuditStatus());
                num = mateCourseMapper.updateCourse(mtCourseCheck);
                MateProductionRecord mateProductionRecord = new MateProductionRecord();
                mateProductionRecord.setIsCreateCheck("0");
                mateProductionRecord.setMtRecordId(mtCourseCheck.getMtRecordId());
                mtRecordMapper.editRecord(mateProductionRecord);
            }

        } else if (mtAudit1.getOrderType().equals("11")) {
            if (mtAudit.getAuditStatus().equals("1")) {
                MateTransfer mateTransfer = mateTransferMapper.selectMateTransfer(mtAudit1.getOrderId());
                if (mtAudit.getAuditStatus() != null && mtAudit.getAuditStatus().equals("")) {
                    mtAudit.setAuditTime(DateUtils.getNowDate());
                }
                mateTransfer.setTransferStatus(mtAudit.getAuditStatus());
                num = mateTransferMapper.updateMateTransfer(mateTransfer);
            } else {
                MateTransfer mateTransfer = mateTransferMapper.selectMateTransfer(mtAudit1.getOrderId());
                mateTransfer.setTransferStatus(mtAudit.getAuditStatus());
                num = mateTransferMapper.updateMateTransfer(mateTransfer);

            }
        } else if (mtAudit1.getOrderType().equals("12")) {
            if (mtAudit.getAuditStatus().equals("1")) {
                MateCleanRecord mateCleanRecord = mateCleanRecordMapper.selectCleanRecord(mtAudit1.getOrderId());
                if (mtAudit.getAuditStatus() != null && mtAudit.getAuditStatus().equals("")) {
                    mtAudit.setAuditTime(DateUtils.getNowDate());
                }
                mateCleanRecord.setCleanStatus(mtAudit.getAuditStatus());
                num = mateCleanRecordMapper.updateMateCleanRecord(mateCleanRecord);
                MtCourseCheck mtCourseCheck = new MtCourseCheck();
                mtCourseCheck.setMtCourseId(mateCleanRecord.getMtCourseId());
                mtCourseCheck.setIsCreateCleaning("1");
                mateCourseMapper.updateCourse(mtCourseCheck);

            } else {
                MateCleanRecord mateCleanRecord = mateCleanRecordMapper.selectCleanRecord(mtAudit1.getOrderId());
                mateCleanRecord.setCleanStatus(mtAudit.getAuditStatus());
                num = mateCleanRecordMapper.updateMateCleanRecord(mateCleanRecord);
                MtCourseCheck mtCourseCheck = new MtCourseCheck();
                mtCourseCheck.setMtCourseId(mateCleanRecord.getMtCourseId());
                mtCourseCheck.setIsCreateCleaning("0");
                mateCourseMapper.updateCourse(mtCourseCheck);
            }

        } else if (mtAudit1.getOrderType().equals("13")) {
            if (mtAudit.getAuditStatus().equals("1")) {
                CheckInformation checkInformation = checkInformationMapper.selectInformation(mtAudit1.getOrderId());
                if (mtAudit.getAuditStatus() != null && mtAudit.getAuditStatus().equals("")) {
                    mtAudit.setAuditTime(DateUtils.getNowDate());
                }
                checkInformation.setInformationStatus(mtAudit.getAuditStatus());
                num = checkInformationMapper.updateInformation(checkInformation);
            } else {
                CheckInformation checkInformation = checkInformationMapper.selectInformation(mtAudit1.getOrderId());
                checkInformation.setInformationStatus(mtAudit.getAuditStatus());
                num = checkInformationMapper.updateInformation(checkInformation);
                MateCleanRecord mateCleanRecord = new MateCleanRecord();
                mateCleanRecord.setIsCreatDaily("0");
                mateCleanRecordMapper.updateMateCleanRecord(mateCleanRecord);
            }
        } else if (mtAudit1.getOrderType().equals("14")) {
            if (mtAudit.getAuditStatus().equals("1")) {
                ProductReport productReport = productReportMapper.selectReport(mtAudit1.getOrderId());
                if (mtAudit.getAuditStatus() != null && mtAudit.getAuditStatus().equals("")) {
                    mtAudit.setAuditTime(DateUtils.getNowDate());
                }
                productReport.setReportStatus(mtAudit.getAuditStatus());
                num = productReportMapper.updateReport(productReport);
            } else {
                ProductReport productReport = productReportMapper.selectReport(mtAudit1.getOrderId());
                productReport.setReportStatus(mtAudit.getAuditStatus());
                num = productReportMapper.updateReport(productReport);
            }
        } else if (mtAudit1.getOrderType().equals("16")) {
            if (mtAudit.getAuditStatus().equals("1")) {
                ReadyProcurement readyProcurement = readyMapper.selectReadyProcurement(mtAudit1.getOrderId());
                if (mtAudit.getAuditStatus() != null && mtAudit.getAuditStatus().equals("")) {
                    mtAudit.setAuditTime(DateUtils.getNowDate());
                }
                readyProcurement.setReadyStatus(mtAudit.getAuditStatus());
                num = readyMapper.updateReadyProcurement(readyProcurement);
            } else {
                ReadyProcurement readyProcurement = readyMapper.selectReadyProcurement(mtAudit1.getOrderId());
                readyProcurement.setReadyStatus(mtAudit.getAuditStatus());
                num = readyMapper.updateReadyProcurement(readyProcurement);
            }
        }
        if (num == 1) {
            return mtAuditMapper.updateAudit(mtAudit);
        }
        return num;
    }

    @Override
    public List<MtAudit> selectAuditList(MtAudit mtAudit) {
        return mtAuditMapper.selectAuditList(mtAudit);
    }

    @Override
    public int returnReview(Long auditId) {
        SysUser sysUser = SecurityUtils.getLoginUser().getUser();
        MtAudit mtAudit = mtAuditMapper.selectAuditById(auditId);
        Long deptId = sysUser.getCompanyId();
        Long orderId = mtAudit.getOrderId();
        String orderType = mtAudit.getOrderType();
        String auditStatus = mtAudit.getAuditStatus();
        mtAudit.setAuditRemark("");
        mtAudit.setAuditStatus("0");
        int num = 0;
        if (orderType.equals("0")) {
            MateCheck mateCheck = mateCheckMapper.selectMateCheckByOrderId(orderId);
            if (mateCheck.getIsCreateProject().equals("1")) {
                return 0;
            }
            mateCheck.setCheckStatus("0");
            num = mateCheckMapper.updateCheckInformation(mateCheck);
            ReadyProcurement readyProcurement = new ReadyProcurement();
            readyProcurement.setIsCreatCheck("0");
            readyProcurement.setReadyId(mateCheck.getReadyId());
            readyMapper.updateReadyProcurement(readyProcurement);
        } else if (orderType.equals("1")) {
            MateProductCheck mateProductCheck = checkReportMapper.selectMateProductCheckById(orderId);
            if (mateProductCheck.getIsMtWarehousing().equals("1")) {
                return 0;
            }
            mateProductCheck.setMpcheckStatus("0");
            num = checkReportMapper.updateMateProductCheck(mateProductCheck);
            MateCheck mateCheck = mateCheckMapper.selectMateCheckByOrderId(mateProductCheck.getCkInformationId());
            mateCheck.setCkInformationId(mateProductCheck.getCkInformationId());
            mateCheck.setIsCreateProject("0");
            mateCheckMapper.updateCheckInformation(mateCheck);
            ReadyProcurement readyProcurement = new ReadyProcurement();
            readyProcurement.setAuditCheck("0");
            readyProcurement.setReadyId(mateCheck.getReadyId());
            readyMapper.updateReadyProcurement(readyProcurement);
        } else if (orderType.equals("3")) {
            MateInventory mateInventory = mateInventoryMapper.selectEdit(orderId);
            List<Long> list = new ArrayList<>();
            if (mateInventory.getMateInventoryModels().size() != 0) {
                for (MateInventoryModel mateInventoryModel : mateInventory.getMateInventoryModels()) {
                    if (mateInventoryModel.getIsCheck().equals("1")) {
                        list.add(mateInventoryModel.getInventoryNo());
                    }
                }
                if (list.size() == 0) {
                    mateInventory.setInventoryStatus("0");
                    num = mateInventoryMapper.updateMateInventory(mateInventory);
                } else {
                    return 0;
                }
            } else {
                return 0;
            }

        } else if (orderType.equals("5")) {
            MateRequistion mateRequistion = mateRequistionMapper.selectMateRequistion(orderId);
            mateRequistion.setRqStatus("0");
            num = mateRequistionMapper.updateMateRequistion(mateRequistion);
        } else if (orderType.equals("6")) {
            MateThrough mateThrough = mateThroughMapper.selectMateThrough(orderId);
            mateThrough.setThroughStatus("0");
            num = mateThroughMapper.updatemateThrough(mateThrough);
            List<MateRequistionModel> list = mateRequistionMapper.selectMateRequistionModel(mateThrough.getRequistionModelId());
            for (MateRequistionModel mateRequistionModel : list) {
           /*     MateWarehousing warehousing = new MateWarehousing();
                warehousing.setMateproductId(mateRequistionModel.getMateproductId());
                warehousing.setDeptId(mateRequistionModel.getDeptId());
                warehousing.setMateBatch(mateRequistionModel.getMateBatch());
                warehousing.setMateProductType(mateRequistionModel.getMateProductType());
                MateWarehousing warehousing1 = warehousingMapper.selectMateWarehousing(warehousing);
                if (warehousing1 != null) {
                    int warehousingNum = warehousing1.getWarehousingNum();
                    int num1 = warehousingNum + realityNum;
                    warehousing.setWarehousingNum(num1);
                    warehousing.setWarehousingId(warehousing1.getWarehousingId());
                    if (warehousing.getWarehousingNum() < 0) {
                        new AjaxResult(AjaxResult.Type.ERROR, "库存不足");
                        throw new RuntimeException("库存不足");
                    }
                    warehousingMapper.updateMateWarehousing(warehousing);
                } else {
                    new AjaxResult(AjaxResult.Type.ERROR, "没有找到该型号的数据，请确定是否有该型号库存");
                    throw new RuntimeException("没有找到该型号的数据，请确定是否有该型号库存");
                }
                warehousingRecordsMapper.deleteWarehousingRecords(mateRequistionModel.getRequistionModelId().toString());*/
         //       mateRequistionModel.setRealityNum(mateRequistionModel.getRealityNum() + mateThrough.getRealityNum());
                mateRequistionMapper.updateMateRequistionModel(mateRequistionModel);
            }
        } else if (orderType.equals("7")) {
            MateProductionPlan mateProductionPlan = mtProductionMapper.selectPlan(orderId);
            mateProductionPlan.setPlanStatus("0");
            num = mtProductionMapper.editPlan(mateProductionPlan);
        } else if (orderType.equals("8")) {
            MateProductionRecord mateProductionRecord = mtRecordMapper.selectRecord(orderId);
            MateWarehousing mateWarehousing = new MateWarehousing();
            for (MateRecordInformation mateRecordInformation : mateProductionRecord.getMateRecordInformations()) {
                String mateproductType = mateRecordInformation.getMateProductType();
                String enterNum = mateRecordInformation.getEnterNum();
                int allNum = Math.toIntExact(mateRecordInformation.getNeedNum());
                mateWarehousing.setDeptId(deptId);
                mateWarehousing.setEnterNum(enterNum);
                mateWarehousing.setMateProductType(mateproductType);
                mateWarehousing.setWarehousingId(mateRecordInformation.getWarehousingId());
                mateWarehousing.setMateproductId(mateRecordInformation.getMateproductId());
                mateWarehousing.setWarehousingNum(allNum);
                MateWarehousing mateWarehousing1 = warehousingMapper.selectMateWarehousing(mateWarehousing);
                if (mateWarehousing1 == null) {
                    warehousingMapper.insertMateWarehousing(mateWarehousing);
                } else {
                    int warehousingNum = mateWarehousing1.getWarehousingNum();
                    warehousingNum = warehousingNum + allNum;
                    mateWarehousing.setWarehousingNum(warehousingNum);
                    mateWarehousing.setWarehousingId(mateWarehousing1.getWarehousingId());
                    warehousingMapper.updateMateWarehousing(mateWarehousing);
                }
            }

            mateProductionRecord.setRecordStatus("0");
            num = mtRecordMapper.editRecord(mateProductionRecord);

            MateTransfer mateTransfer = new MateTransfer();
            mateTransfer.setTransferId(mateProductionRecord.getTransferId());
            mateTransfer.setIsCreateRecord("0");
            mateTransferMapper.updateMateTransfer(mateTransfer);

        } else if (orderType.equals("9")) {
            MateDaily mateDaily = mateDailyMapper.selectMateDaily(orderId);
            mateDaily.setDailyStatus(mateDaily.getDailyStatus());
            num = mateDailyMapper.editMatedaily(mateDaily);

        } else if (orderType.equals("10")) {
            MtCourseCheck mtCourseCheck = mateCourseMapper.selectMateCourse(orderId);
            mtCourseCheck.setCourseStatus("0");
            num = mateCourseMapper.updateCourse(mtCourseCheck);

            MateProductionRecord mateProductionRecord = new MateProductionRecord();
            mateProductionRecord.setIsCreateCheck("0");
            mateProductionRecord.setMtRecordId(mtCourseCheck.getMtRecordId());
            mtRecordMapper.editRecord(mateProductionRecord);

        } else if (orderType.equals("11")) {
            MateTransfer mateTransfer = mateTransferMapper.selectMateTransfer(orderId);
            mateTransfer.setTransferStatus("0");
            num = mateTransferMapper.updateMateTransfer(mateTransfer);

        } else if (orderType.equals("12")) {
            MateCleanRecord mateCleanRecord = mateCleanRecordMapper.selectCleanRecord(orderId);
            mateCleanRecord.setCleanStatus("0");
            num = mateCleanRecordMapper.updateMateCleanRecord(mateCleanRecord);

            MtCourseCheck mtCourseCheck = new MtCourseCheck();
            mtCourseCheck.setMtCourseId(mateCleanRecord.getMtCourseId());
            mtCourseCheck.setIsCreateCleaning("0");
            mateCourseMapper.updateCourse(mtCourseCheck);

        } else if (orderType.equals("13")) {
            CheckInformation checkInformation = checkInformationMapper.selectInformation(mtAudit.getOrderId());
            checkInformation.setInformationStatus("0");
            num = checkInformationMapper.updateInformation(checkInformation);
            MateCleanRecord mateCleanRecord = new MateCleanRecord();
            mateCleanRecord.setIsCreatDaily("0");
            mateCleanRecordMapper.updateMateCleanRecord(mateCleanRecord);

        } else if (orderType.equals("14")) {
            ProductReport productReport = productReportMapper.selectReport(mtAudit.getOrderId());
            productReport.setReportStatus("0");
            num = productReportMapper.updateReport(productReport);
        } else if (orderType.equals("16")) {
            ReadyProcurement readyProcurement = readyMapper.selectReadyProcurement(mtAudit.getOrderId());
            readyProcurement.setReadyStatus("0");
            num = readyMapper.updateReadyProcurement(readyProcurement);
        }
        if (num != 0) {
            return mtAuditMapper.updateAudit(mtAudit);
        } else {
            return num;
        }
    }

}
